//
//  L_SongListViewController.m
//  音悦咖
//
//  Created by dllo on 15/7/16.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "L_SongListViewController.h"
#import "D_MyMusicCollectionViewCell.h"
#import "D_AlerterUIview.h"
#import "D_BgImageViewController.h"
#import "AFNetworkHandler.h"
#import "UMSocial.h"
#import "D_DataBase.h"
#import "D_MyMusic.h"
#import "C_HeaderFirstCollectionViewCell.h"
#import "C_HeaderSecondCollectionViewCell.h"
#import "L_footPlaytabBar.h"
#import "MBProgressHUD.h"
@interface L_SongListViewController () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout, MBProgressHUDDelegate>

@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, assign) NSInteger footerHight;
@property (nonatomic, assign) NSInteger sectionNum;
@property (nonatomic, assign) NSInteger tagNum;
@property (nonatomic, retain) NSMutableArray *songListArr;
@property (nonatomic, retain) NSString *idStr;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, retain) UITapGestureRecognizer *footPlayTap;
@property (nonatomic, retain) UIScrollView *headScroll;
@property (nonatomic, retain) L_SongListModel *songListModel;
@property (nonatomic, retain) MBProgressHUD *mbProgress;


@end

@implementation L_SongListViewController


- (void)dealloc
{
    [_mbProgress release];
    [_songId release];
    [_collection release];
    [_songListArr release];
    [_idStr release];
    [_footPlayTap release];
    [_headScroll release];
    [_songListModel release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *backImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    backImg.image = [UIImage imageNamed:@"musicbg.jpg"];
    backImg.alpha = 0.8;
    [self.view addSubview:backImg];
    [backImg release];
    
    self.single = [L_Singleton shareManager];
    
    self.index = 0;
    [self judgeConnection];
    [self getData];
    
    [self createView];
    
    
}

#pragma mark - 判断是否有网络连接
- (void)judgeConnection
{
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
        if ([AFStringFromNetworkReachabilityStatus(status) isEqualToString:@"Not Reachable"]) {
            
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前网络没有连接" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            [alert release];
        } else if ([AFStringFromNetworkReachabilityStatus(status) isEqualToString:@"Reachable via WWAN"]){
            
            [self createProgress];
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前正使用数据流量" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
//            [alert show];
//            [alert release];
        }else if ([AFStringFromNetworkReachabilityStatus(status) isEqualToString:@"Reachable via WiFi"]){
            
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前网络为wifi网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
//            [alert show];
//            [alert release];
            [self createProgress];
        }
        
    }];
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
//    [self.collection reloadData];
    
}

#pragma mark - 获取数据
- (void)getData
{
    NSString *str = [NSString stringWithFormat:@"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%%5B%@%%5D&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=8.0&active=1&net=2&openudid=b55e125b1cbcd2f8d637e5573f9a6f6f241e0c3f&idfa=DD02B768-7FEA-48C3-A977-931093C926A0&utdid=VZ5%%2FNAYMGNwDAGGSKGx50btO&alf=201200&bundle_id=com.ttpod.music", self.songId];

    
    [AFNetworkHandler GETWithAFNByURL:str completion:^(id result) {
        NSMutableArray *tempArr = [NSMutableArray array];
        tempArr = [result objectForKey:@"data"];
        NSDictionary *dic = [tempArr lastObject];
        self.songListModel = [[L_SongListModel alloc] initWithDic:dic];
        
        for (NSMutableDictionary *tempDic in [self.songListModel songlist]) {
            NSString *tempStr = [tempDic objectForKey:@"song_id"];
            self.idStr = [NSString stringWithFormat:@"%@,%@", self.idStr, tempStr];
        }
        
        NSString *str1 = [NSString stringWithFormat:@"http://ting.hotchanson.com/songs/downwhite?song_id=%@", self.idStr];
        
        [AFNetworkHandler GETWithAFNByURL:str1 completion:^(id result) {
            NSArray *tempArr1 = [result objectForKey:@"data"];
            
            self.songListArr = [NSMutableArray array];
            // 遍历参数数组
            for (NSDictionary *dic in tempArr1) {
          
                    // 创建对象
                    L_RecommendPlayModel *model = [L_RecommendPlayModel baseModelWithDic:dic];
                    model.url = [[[dic objectForKey:@"url_list"] lastObject] objectForKey:@"url"];
                    // 添加数组
                    [self.songListArr addObject:model];
            }
            [self.collection reloadData];
        }];
        
        self.titleLabel.text = self.songListModel.tweet;
        self.mbProgress.hidden = YES;
        [self.collection reloadData];
    }];
    
    
}

// 创建菊花加载页
- (void)createProgress
{
    
    self.mbProgress = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.mbProgress.labelText = @"稍等";
    self.mbProgress.detailsLabelText = @"正在加载…";
    self.mbProgress.delegate = self;
    [self.mbProgress show:YES];
    
    for (UIView *v in self.view.subviews) {
        if (v.tag == 999999) {
            [self.view bringSubviewToFront:v];
        }
    }
    
}

- (void)doSomething
{
    //你要进行的一些事
    sleep(2);
}

#pragma mark - 创建页面
- (void)createView
{
    
    // collection
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH - 20 , 50);
    //    layout.minimumLineSpacing = 30;
    
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
#pragma mark - footer
    //    layout.headerReferenceSize = CGSizeMake(355, 100);
    //    layout.footerReferenceSize = CGSizeMake(300, 50);
    
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:layout];
    self.collection.backgroundColor = [UIColor clearColor];
    self.collection.tag = 20001;
    self.collection.delegate = self;
    self.collection.dataSource = self;
    //self.collection.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    [self.view addSubview:self.collection];
#pragma mark - 注册重用池
    [self.collection registerClass:[D_MyMusicCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    [self.collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    [self.collection release];
    [layout release];
    
    

}

#pragma mark - 头部大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    switch (collectionView.tag) {
        case 20000:
            return CGSizeMake(0, 0);
            break;
            
        case 20001:
            if (section == 0) {
                return CGSizeMake(0, 200);
            }
            return CGSizeMake(0, 0);
            break;
            
        default:
            break;
    }
    
    return CGSizeMake(0, 0);
    
    
}

#pragma mark - 尾部方法协议
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    switch (collectionView.tag) {
        case 20000:
            return CGSizeMake(0, 0);
            break;
            
        case 20001:
            if (section == self.sectionNum) {
                return CGSizeMake(0, self.footerHight);
            }
            return CGSizeMake(0, 0);
            break;
            
        default:
            break;
    }
  
    return CGSizeMake(0, 0);
    
}
#pragma mark - footer/header 协议方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    switch (collectionView.tag) {
        case 20000:
            return nil;
            break;
            
        case 20001:
            
            if (kind == UICollectionElementKindSectionHeader) {
                UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
                header.backgroundColor = [UIColor grayColor];
//                [header.layer setCornerRadius:20];
                header.alpha = 0.8;
                
                for (UIView *v in header.subviews) {
                    [v removeFromSuperview];
                }
                
                blackGroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, header.bounds.size.width, header.bounds.size.height)];
                blackGroundImageView.backgroundColor = [UIColor greenColor];
                [blackGroundImageView setImageWithURL:[NSURL URLWithString:[self.songListModel.pics lastObject]] placeholderImage:[UIImage imageNamed:@"backgound..png"]];
                [header addSubview:blackGroundImageView];
                [blackGroundImageView release];
                
                
                headerLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
                headerCollection = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200) collectionViewLayout:headerLayout] autorelease];
                headerLayout.minimumLineSpacing = 0;
                headerCollection.pagingEnabled = YES;
                headerLayout.itemSize = CGSizeMake(SCREEN_WIDTH, headerCollection.frame.size.height);
                headerLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
                headerCollection.delegate = self;
                headerCollection.dataSource = self;
                headerCollection.tag = 20000;
                headerCollection.backgroundColor = [UIColor clearColor];
                [header addSubview:headerCollection];
                
                
#pragma mark Cell重用池
                [headerCollection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"temp"];
                [headerCollection registerClass:[C_HeaderFirstCollectionViewCell class] forCellWithReuseIdentifier:@"first"];
                [headerCollection registerClass:[C_HeaderSecondCollectionViewCell class] forCellWithReuseIdentifier:@"second"];
                
                
                
                return header;
            }
            
            
            if (kind == UICollectionElementKindSectionFooter) {
                UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
                footer.backgroundColor = [UIColor clearColor];
                
                
                [footer.layer setCornerRadius:10];
                // 分享按钮
                UIButton *share = [UIButton buttonWithType:UIButtonTypeSystem];
                share.frame = CGRectMake(SCREEN_WIDTH / 7, 15, 35, 35);
                share.backgroundColor = [UIColor clearColor];
                share.tag = indexPath.section;
                [share setImage:[[UIImage imageNamed:@"share"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];
                [share addTarget:self action:@selector(lshareAction:) forControlEvents:UIControlEventTouchUpInside];
                UILabel *shareLable = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 7, 55, 50, 20)];
                shareLable.text = @"分享";
                shareLable.font = [UIFont fontWithName:nil size:14];
                [footer addSubview:shareLable];
                [footer addSubview:share];
                [shareLable release];
                
                // 我喜欢按钮
                UIButton *myLovew = [UIButton buttonWithType:UIButtonTypeSystem];
                myLovew.frame = CGRectMake(SCREEN_WIDTH / 2 - 17.5, 15, 35, 35);
                myLovew.backgroundColor = [UIColor clearColor];
                [myLovew setImage:[[UIImage imageNamed:@"mylove"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];
                [myLovew addTarget:self action:@selector(myLoveAction:) forControlEvents:UIControlEventTouchUpInside];
                UILabel *myLovewLable = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 17.5, 55, 50, 20)];
                myLovewLable.text = @"我喜欢";
                myLovew.tag =  indexPath.section;
                myLovewLable.font = [UIFont fontWithName:nil size:14];
                [footer addSubview:myLovewLable];
                [footer addSubview:myLovew];
                [myLovewLable release];
                
                // 收藏
                UIButton *collect = [UIButton buttonWithType:UIButtonTypeSystem];
                collect.frame = CGRectMake(SCREEN_WIDTH / 7 * 6 - 35, 15, 35, 35);
                collect.backgroundColor = [UIColor clearColor];
                [collect setImage:[[UIImage imageNamed:@"save"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];
                [collect addTarget:self action:@selector(collectAction:) forControlEvents:UIControlEventTouchUpInside];
                UILabel *collectLable = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 7 * 6 - 35, 55, 50, 20)];
                collectLable.text = @"收藏";
                collect.tag = indexPath.section;
                collectLable.font = [UIFont fontWithName:nil size:14];
                [footer addSubview:collectLable];
                [footer addSubview:collect];
                [collectLable release];
                
                
                return footer;
            }

            break;
            
        default:
            break;
    }
    
        return nil;
}

#pragma mark - 分享
- (void)lshareAction:(UIButton *)button
{
//    [D_AlerterUIview showAlert:@"分享成功!"];
    NSString *url = [[[self.songListArr[button.tag] url_list] firstObject] objectForKey:@"url"];
    NSString *song = [self.songListArr[button.tag] song_name];
    NSString *str = [NSString stringWithFormat:@"请输入要分享的文字.......%@%@",url,song];
    [UMSocialSnsService presentSnsIconSheetView:self appKey:SDK_KEY shareText:str shareImage:[UIImage imageNamed:@"headimg"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToQzone,UMShareToQQ,nil] delegate:nil];
    
    [UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ,UMShareToQzone,UMShareToWechatTimeline]];

}

#pragma mark - 我喜欢
- (void)myLoveAction:(UIButton *)button
{
    NSInteger num = button.tag;
    
    D_DataBase *data = [[D_DataBase alloc]init];
    [data openDB];
    D_MyMusic *myMusic = [[D_MyMusic alloc] init];
    myMusic.singer_name = [self.songListArr[num] singer_name];
    myMusic.song_name = [self.songListArr[num] song_name];
    myMusic.url = (NSString *)[self.songListArr[num] url];
    myMusic.pic_url = [[[self.songListArr[num] mv_list] firstObject] objectForKey:@"pic_url"];
    myMusic.song_id = [self.songListArr[num] song_id];
    myMusic.duration_time = [[[self.songListArr[num] mv_list] firstObject] objectForKey:@"duration"];
    
    NSArray *tempArr = [NSArray array];
   tempArr = [data selectInfor];
    NSInteger tempNum = 0;
    for (NSInteger i = 0; i < tempArr.count; i++) {
        if ([[tempArr[i] song_name] isEqualToString:[self.songListArr[num] song_name]]) {
            tempNum ++;
            [D_AlerterUIview showAlert:@"已经添加我喜欢!"];
        }
    }
    if (tempNum == 0) {
         [D_AlerterUIview showAlert:@"我喜欢!"];
        [data insertInforMyMusic:myMusic];
    }
    
  
    
}
#pragma mark - 收藏
- (void)collectAction:(UIButton *)collect
{
    NSInteger num = collect.tag;
    [D_AlerterUIview showAlert:@"收藏成功!"];
    D_DataBase *data = [[D_DataBase alloc]init];
    [data openDB];
     D_MyMusic *myMusic = [[D_MyMusic alloc] init];
    myMusic.singer_name = [self.songListArr[num] singer_name];
    myMusic.song_name = [self.songListArr[num] song_name];
    myMusic.url = (NSString *)[self.songListArr[num] url];
    myMusic.pic_url = [[[self.songListArr[num] mv_list] firstObject] objectForKey:@"pic_url"];
    myMusic.song_id = [self.songListArr[num] song_id];
    myMusic.duration_time = [[[self.songListArr[num] mv_list] firstObject] objectForKey:@"duration"];
    
    NSArray *tempArr = [NSArray array];
    tempArr = [data selectCollect];
    NSInteger tempNum = 0;
    for (NSInteger i = 0; i < tempArr.count; i++) {
        if ([[tempArr[i] song_name] isEqualToString:[self.songListArr[num] song_name]]) {
            tempNum ++;
            [D_AlerterUIview showAlert:@"已经收藏!"];
        }
    }
    if (tempNum == 0) {
        [D_AlerterUIview showAlert:@"收藏成功!"];
        [data insertMyCollect:myMusic];
    }
    
    
    
    
}
#pragma mark - 删除
- (void)deleteAction
{
    
    UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否删除" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [aler show];
    [aler release];
    
}

#pragma mark - collection协议方法
// section 个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    switch (collectionView.tag) {
        case 20000:
            return 3;
            break;
            
        default:
            break;
    }
    return self.songListArr.count;
}
// section个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}
// cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 20000:
        {
            switch (indexPath.item) {
                case 0:{
                    
                    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"temp" forIndexPath:indexPath];
                    cell.backgroundColor = [UIColor clearColor];
                    return cell;
                    
                    break;
                }
                case 1:{
                    C_HeaderFirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"first" forIndexPath:indexPath];
                    cell.backgroundColor = RGBA(100, 200, 100, 0.8);
                    cell.imageView.image = [UIImage imageNamed:@"headimg"];
                    cell.nameLabel.text = [self.songListModel songlistname];
                    cell.countLabel.text = [self.songListModel favorite_count];
        
                    return cell;
                    break;
                }
                case 2:{
                    C_HeaderSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"second" forIndexPath:indexPath];
                    cell.backgroundColor = RGBA(180, 180, 180, 0.5);
          
                    return cell;
                    break;
                }
                default:
                    break;
            }

        }
            break;
            
        case 20001:
            {
                
                
                D_MyMusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
                cell.alpha = 0.8;
                cell.backgroundColor = [UIColor clearColor];
                [cell.layer setBorderWidth:2];
                [cell.layer setBorderColor:[UIColor grayColor].CGColor];
                
                
                cell.recommendPlayModel = self.songListArr[indexPath.section];
            
                // 控制尾部buuton
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(SCREEN_WIDTH * 0.8, 20, 20, 20);
                button.backgroundColor = [UIColor clearColor];
                button.tag = indexPath.section;
                
                [button setBackgroundImage:[UIImage imageNamed:@"footerdown"] forState:UIControlStateNormal];
                
                
                [button addTarget:self action:@selector(footerAction:) forControlEvents:UIControlEventTouchUpInside];
                
                [cell addSubview:button];
                [cell.layer setCornerRadius:10];
                return  cell;
            }
            break;
            
        default:
            break;
    }
    
            return nil;
}

// item选择方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    // 歌曲播放
    L_Singleton *single = [L_Singleton shareManager];
    L_footPlaytabBar *footTabBar = [L_footPlaytabBar shareTabBar];
    // 定时器开启
    [footTabBar.time setFireDate:[NSDate distantPast]];
    // 传值
    single.song_id = [self.songListArr[indexPath.section] song_id];
    single.index = indexPath.section;
    switch (collectionView.tag) {
        case 20001:
         
            
            // 临时列表
            if (single.array == nil) {
                for (PlayerModel *play in self.songListArr) {
                    [single.array addObject:play];
                    
                }
                NSString *url = [[[single.array[indexPath.section] url_list] firstObject] objectForKey:@"url"];
                [single playWithUrl:url];
                self.single.url = url;
                self.single.playUrl = url;
                single.state = @"playing";
                single.index = indexPath.section;
        
                footTabBar.footPlayView.songName.text = [single.array[indexPath.section] song_name];
                footTabBar.footPlayView.songerName.text = [single.array[indexPath.section] singer_name];
                [footTabBar.footPlayView.mainImage setImageWithURL:[NSURL URLWithString:[[[single.array[indexPath.section] mv_list] firstObject] objectForKey:@"pic_url"]] placeholderImage:[UIImage imageNamed:@"lloo.png"]];
                [footTabBar.footPlayView.playOrPause setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
                
                
            } else if ([single.array firstObject] != [self.songListArr firstObject]) {
               // single.array = NULL;
                single.array = [NSMutableArray array];
                for (PlayerModel *play in self.songListArr) {
                    [single.array addObject:play];
                    
                }
                
                NSString *url = [[[single.array[indexPath.section] url_list] firstObject] objectForKey:@"url"];
                
                [single playWithUrl:url];
                self.single.url = url;
                self.single.playUrl = url;
                single.state = @"playing";
                single.index = indexPath.section;
                footTabBar.footPlayView.songName.text = [single.array[indexPath.section] song_name];
                footTabBar.footPlayView.songerName.text = [single.array[indexPath.section] singer_name];
                [footTabBar.footPlayView.mainImage setImageWithURL:[NSURL URLWithString:[[[single.array[indexPath.section] mv_list] firstObject] objectForKey:@"pic_url"]] placeholderImage:[UIImage imageNamed:@"lloo.png"]];
                [footTabBar.footPlayView.playOrPause setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
            } else {
                single.song_id = [single.array[indexPath.section] song_id];
                NSString *url = [[[single.array[indexPath.section] url_list] firstObject] objectForKey:@"url"];
                [single playWithUrl:url];
                self.single.url = url;
                self.single.playUrl = url;
                single.state = @"playing";
                single.index = indexPath.section;
                footTabBar.footPlayView.songName.text = [single.array[indexPath.section] song_name];
                footTabBar.footPlayView.songerName.text = [single.array[indexPath.section] singer_name];
                [footTabBar.footPlayView.mainImage setImageWithURL:[NSURL URLWithString:[[[single.array[indexPath.section] mv_list] firstObject] objectForKey:@"pic_url"]] placeholderImage:[UIImage imageNamed:@"lloo.png"]];
                [footTabBar.footPlayView.playOrPause setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
            }
            
            
            // 插入到本地歌曲数据库
            D_MyMusic *myMusic = [[D_MyMusic alloc] init];
            myMusic.song_name = [self.songListArr[indexPath.section] song_name];
            myMusic.singer_name = [self.songListArr[indexPath.section] singer_name];
            myMusic.url = (NSString *)[self.songListArr[indexPath.section] url];
            myMusic.pic_url = [[[self.songListArr[indexPath.section] mv_list] firstObject] objectForKey:@"pic_url"];
            myMusic.duration_time = [[[self.songListArr[indexPath.section] url_list] firstObject] objectForKey:@"duration"];
            NSLog(@"pic_url = %@", [[[self.songListArr[indexPath.section] mv_list] firstObject] objectForKey:@"pic_url"]);
            
            myMusic.song_id = [self.songListArr[indexPath.section] song_id];
             D_DataBase *data = [[D_DataBase alloc]init];
            [data openDB];
            NSArray *tempArr = [NSArray array];
             tempArr = [data selectLocal];
            NSInteger tempNum = 0;
            for (NSInteger i = 0;  i < tempArr.count; i++) {
                if ([[tempArr[i] song_name] isEqualToString:[self.songListArr[indexPath.section] song_name]]) {
                    tempNum = 1;
                }
            }
            if (tempNum == 0) {
                [data openDB];
                [data insertLocalMyMusic:myMusic];
            }
            
            break;
            
        default:
            break;
    }
    
  
}

#pragma mark - 按钮显示footer
- (void)footerAction:(UIButton *)button
{
    
    button.selected = !button.selected;
//    
//    if (button.selected) {
//        [button setBackgroundImage:[UIImage imageNamed:@"footerup"] forState:UIControlStateNormal];
//        
//    }else{
//        [button setBackgroundImage:[UIImage imageNamed:@"footerdown"] forState:UIControlStateNormal];
//    }
    if (button.tag == self.tagNum) {
        self.footerHight = 0;
        self.tagNum = 1111110;
//        [button setBackgroundImage:[UIImage imageNamed:@"footerup"] forState:UIControlStateNormal];
        
    }else{
        
//        [button setBackgroundImage:[UIImage imageNamed:@"footerdown"] forState:UIControlStateNormal];
        self.footerHight = 80;
        self.tagNum = button.tag;
    }
    self.sectionNum = button.tag;
    [self.collection reloadData];
    
}




// 返回按钮
- (void)backAction
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
